﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;

namespace ModernShopping.AppCode
{
    public class DbProductComment
    {
        public static void InitGridView(GridView gv, int ProductCode)
        {
            ModernShoppingDataContext db = new ModernShoppingDataContext();
            var result = db.ProductComnents
                .Where(c => c.ProductCode.Equals(ProductCode) && c.isDelete == false).OrderByDescending(c => c.Time);
            gv.DataSource = result;
            gv.DataBind();
        }

        public static List<ProductComnent> getList(int ProductCode, int take)
        {
            ModernShoppingDataContext db = new ModernShoppingDataContext();
            var result = db.ProductComnents
               .Where(c => c.ProductCode.Equals(ProductCode) && c.isDelete == false).OrderByDescending(c => c.Time)
               .Take(take);
            return result.ToList<ProductComnent>();
        }

        public static bool Comment(string comment, string username, int ProductCode)
        {
            ModernShoppingDataContext db = new ModernShoppingDataContext();
            ProductComnent productComent = new ProductComnent();
            productComent.ProductCode = ProductCode;
            productComent.Time = DateTime.Now;
            productComent.UserName = username;
            productComent.Comment = comment;
            productComent.isDelete = false;
            try
            {
                db.ProductComnents.InsertOnSubmit(productComent);
                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}